<?php

class P2C_Project {

	var $initial = false;

	var $id;
    var $external_id;
    var $platform_id;
	var $name;
	var $url;
    var $image;
    var $active;
    var $description;
    var $approved;
    var $approved_by;
    var $import_date;
    var $batch_id;
    var $update_date;
    var $platform;
    var $projectdata;
    var $currency;

	public static function find( $args = '' ) {
        global $wpdb;
        
		$defaults = array(
			'post_status' => 'any',
			'posts_per_page' => -1,
			'offset' => 0,
			'orderby' => 'id',
			'order' => 'ASC' );

		$args = wp_parse_args( $args, $defaults );
        $order_by = $args['orderby'] . ' ' . $args['order'];

		$projects = $wpdb->get_results($wpdb->prepare("SELECT pr.id,pr.external_id,pr.platform_id,pr.name,pr.url,pr.image,pr.active,pr.description,pr.approved,pr.approved_by,pr.import_date,pr.batch_id,pr.update_date, pl.name as platform, pr.currency
        FROM p2c_platform AS pl JOIN p2c_project AS pr ON pl.id = pr.platform_id 
        ORDER BY $order_by
        LIMIT %d,%d;", $args['offset'], $args['posts_per_page']));

		$objs = array();

		foreach ( $projects as $project )
			$objs[] = new self( $project );

		return $objs;
	}
    
    public static function findBy( $args, $platformId, $country, $showInactive, $showFunded ) {
        global $wpdb;
        
		$defaults = array(
			'post_status' => 'any',
			'posts_per_page' => -1,
			'offset' => 0,
			'orderby' => 'id',
			'order' => 'ASC' );

		$args = wp_parse_args( $args, $defaults );
        $order_by = $args['orderby'] . ' ' . $args['order'];

		$projects = $wpdb->get_results($wpdb->prepare( "SELECT pr.id,pr.external_id,pr.platform_id,pr.name,pr.url,pr.image,pr.active,pr.description,pr.approved,pr.approved_by,pr.import_date,pr.batch_id,pr.update_date, pl.name as platform, pr.currency, d1.id as project_data_id, d1.target, d1.current, d1.current / d1.target as percentage
        FROM p2c_platform AS pl JOIN p2c_project AS pr ON pl.id = pr.platform_id 
        JOIN p2c_project_data AS d1 ON pr.id = d1.project_id AND d1.end_date IS NULL
        LEFT JOIN p2c_project_data AS d2 ON pr.id = d2.project_id AND d2.end_date = DATE_ADD(d1.start_date, INTERVAL 1 SECOND)  
        WHERE (pr.active = 1 OR 1 = %d)
        AND (d1.target > d1.current OR 1 = %d)
        AND ('%s' IN (pl.country) OR '' = '%s')
        AND (%d = pl.Id OR 0 = %d)
        AND (pl.active = 1)
        ORDER BY $order_by 
        LIMIT %d,%d;", $showInactive, $showFunded, $country, $country, $platformId, $platformId, $args['offset'], $args['posts_per_page']));

		$objs = array();

		foreach ( $projects as $project )
			$objs[] = new self( $project );

		return $objs;
	}
    
    public static function countBy( $platformId, $country, $showInactive, $showFunded ) {
        global $wpdb;

		$projects = $wpdb->get_var($wpdb->prepare( "SELECT COUNT(*)
        FROM p2c_platform AS pl JOIN p2c_project AS pr ON pl.id = pr.platform_id 
        JOIN p2c_project_data AS d1 ON pr.id = d1.project_id AND d1.end_date IS NULL
        LEFT JOIN p2c_project_data AS d2 ON pr.id = d2.project_id AND d2.end_date = DATE_ADD(d1.start_date, INTERVAL 1 SECOND)  
        WHERE (pr.active = 1 OR 1 = %d)
        AND (d1.target > d1.current OR 1 = %d)
        AND ('%s' IN (pl.country) OR '' = '%s')
        AND (pl.active = 1)
        AND (%d = pl.Id OR 0 = %d);", $showInactive, $showFunded, $country, $country, $platformId, $platformId));
    
		return $projects ;
	}
    
    
    public static function rowcount( ) {
        global $wpdb;

		$projects = $wpdb->get_var("SELECT COUNT(*) FROM p2c_project;");
        
		return $projects;
	}
    
    public static function get($external_id, $platform, $batch = null){
        global $wpdb;
        
        $projectid = $wpdb->get_var($wpdb->prepare( "SELECT id FROM p2c_project WHERE external_id = %d AND platform_id = %d;",$external_id,$platform->id));
        $project = new self($projectid);
        if($project->initial && $batch) {
            $project->batch_id = $batch->id;   
            $project->import_date = $batch->start_date;
            $project->update_date = $batch->start_date;
            $project->platform_id = $platform->id;
            $project->external_id = $external_id;
        }
        return $project;
    }
    
    public function getData() {
        if($this->id > 0) {
            $this->projectdata = P2C_ProjectData::get($this);
        }
    }

	public function __construct( $project = null ) {
        global $wpdb;
        
		$this->initial = true;
        $this->approved = 0;
        $this->active = 0;
        $this->projectdata = new P2C_ProjectData();
                
        if($project && !is_object($project)) {
            $project = $wpdb->get_row($wpdb->prepare("SELECT pr.id,pr.external_id,pr.platform_id,pr.name,pr.url,pr.image,pr.active,pr.description,pr.approved,pr.approved_by,pr.import_date,pr.batch_id,pr.update_date, pl.name as platform, pr.currency 
            FROM p2c_platform AS pl JOIN p2c_project AS pr ON pl.id = pr.platform_id WHERE pr.id = %d",$project));
        }
        
		if ( $project ) {
			$this->initial = false;
			$this->id = $project->id;
			$this->name = $project->name;
			$this->url = $project->url;
			$this->image = $project->image;
            $this->active = $project->active;
            $this->external_id = $project->external_id;
            $this->platform_id = $project->platform_id;
            $this->platform = $project->platform;
            $this->description = $project->description;
            $this->approved = $project->approved;
            $this->approved_by = $project->approved_by;
            $this->import_date = $project->import_date;
            $this->batch_id = $project->batch_id;
            $this->update_date = $project->update_date;
            $this->currency = $project->currency;
		}
    }
    
	/* Save */

	function save() {
        global $wpdb; 
        
		if ( $this->initial ) {
			$wpdb->insert("p2c_project", array(
               "external_id" => $this->external_id,
                "platform_id" => $this->platform_id,
                "name" => $this->name,
               "url" => $this->url,
               "image" => $this->image,
               "active" => $this->active,
                "description" => $this->description,
                "approved" => $this->approved,
                "approved_by" => $this->approved_by,
                "import_date" => $this->import_date,
                "batch_id" => $this->batch_id,
                "update_date" => $this->update_date,
                "currency" => $this->currency
            ));
            
            $this->initial = false;
            $this->id = $wpdb->insert_id;
		} else {
			$wpdb->update( 
                'p2c_project', 
                array( 
                   "external_id" => $this->external_id,
                    "name" => $this->name,
                   "url" => $this->url,
                   "image" => $this->image,
                   "active" => $this->active,
                    "description" => $this->description,
                    "approved" => $this->approved,
                    "approved_by" => $this->approved_by,
                    "update_date" => date('Y-m-d H:i:s'),
                    "currency" => $this->currency
                ), 
                array( 'id' => $this->id )
            );
		}

		return $this->id;
	}

}


?>